<?php

namespace App\Models\OpenShop\InvestmentPromotion;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use App\Service\Kms\Home\HomeService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class OpenShopOrderTagsMappingModel extends BaseModel
{
    use KfUid;
    use KfSoftDeletes;

    protected $table = 'open_shop_order_tags_mapping';

    protected $fillable = [
        'id',
        'orderNo',
        'tagId',
        'remark',
        'desc',
        'isDeleted',
    ];

    const TAG_TYPE_1 = 1;
    const TAG_TYPE_2 = 2;
    const TAG_TYPE_3 = 3;
    const TAG_TYPE_4 = 4;
    const TAG_TYPE_5 = 5;
    const TAG_TYPE_6 = 6;
    const TAG_TYPE_7 = 7;
    const TAG_TYPE_8 = 8;
    const TAG_TYPE_9 = 9;
//    const TAG_TYPE_10 = 10;
    const TAG_TYPE_11 = 11;
    const TAG_TYPE_12 = 12;
    const TAG_TYPE_13 = 13;
    const TAG_TYPE_14 = 14;
    const TAG_TYPE_15 = 15;
    const TAG_TYPE_16 = 16;
    const TAG_TYPE_17 = 17;
    const TAG_TYPE_18 = 18;
    const TAG_TYPE_19 = 19;
    const TAG_TYPE_20 = 20;


    /**
     * 字段枚举
     *
     * 如果枚举类型变动, 需要通知 @寒烟柔 !!! 不然数据展示会异常
     */
    const TAG_TYPE_MAP_TEXT = [
        self::TAG_TYPE_1  => '筹备正常',
        self::TAG_TYPE_2  => '筹备异常',
        self::TAG_TYPE_3  => '设计待支付',
        self::TAG_TYPE_4  => '设备待支付',
        self::TAG_TYPE_5  => '设备待发货',
        self::TAG_TYPE_6  => '设备部分发货',
        self::TAG_TYPE_7  => '设备全部发货',
        self::TAG_TYPE_8  => '软装待支付',
        self::TAG_TYPE_9  => '软装待发货',
//        self::TAG_TYPE_10 => '软装部分发货',
        self::TAG_TYPE_11 => '软装全部发货',
        self::TAG_TYPE_12 => '商学院待培训',
        self::TAG_TYPE_13 => '商学院已培训',
        self::TAG_TYPE_14 => '活跃客户(正常)',
        self::TAG_TYPE_15 => '待解约',
        self::TAG_TYPE_16 => '暂无计划(沉默)',
        self::TAG_TYPE_17 => '定向选址(正常)',
        self::TAG_TYPE_18 => '高风险流失(沉默)',
        self::TAG_TYPE_19 => '自提工服待确认',
        self::TAG_TYPE_20 => '自提工服待支付',
    ];


    /**
     * 选址客户标签列表-web显示选择框用
     */
    const TAG_LIST_SITE_LOCATION_TEXT = [
        self::TAG_TYPE_1  => '活跃客户(正常)',
        self::TAG_TYPE_17 => '定向选址(正常)',
        self::TAG_TYPE_3  => '暂无计划(沉默)',
        self::TAG_TYPE_18 => '高风险流失(沉默)',
        self::TAG_TYPE_2  => '待解约',
    ];

    /**
     * 选址客户标签列表-导入时判断
     */
    const TAG_LIST_SITE_LOCATION_IMPORT_TEXT = [
        '活跃客户'  => self::TAG_TYPE_14,//活跃客户(正常)
        '定向选址'  => self::TAG_TYPE_17,//定向选址(正常)
        '等待解约'  => self::TAG_TYPE_15,//待解约（默认） - 即：等待解约
        '暂无计划'  => self::TAG_TYPE_16,//暂无计划(沉默)
        '高流失风险' => self::TAG_TYPE_18,//高风险流失(沉默)
    ];

    /**
     * 筹备标签列表
     */
    const TAG_LIST_PREPARE = [
        self::TAG_TYPE_1,
        self::TAG_TYPE_2
    ];

    /**
     * 设计支付标签列表
     */
    const TAG_LIST_DESIGN = [
        self::TAG_TYPE_3
    ];

    /**
     * 设备支付标签列表
     */
    const TAG_LIST_EQUIPMENT_PAY = [
        self::TAG_TYPE_4,
    ];

    /**
     * 设备发货标签列表
     */
    const TAG_LIST_EQUIPMENT_DELIVER = [
        self::TAG_TYPE_5,
        self::TAG_TYPE_6,
        self::TAG_TYPE_7
    ];

    /**
     * 软装支付标签
     */
    const TAG_LIST_SOFT_PAY = [
        self::TAG_TYPE_8,
    ];


    /**
     * 软装发货标签
     */
    const TAG_LIST_SOFT_DELIVER = [
        self::TAG_TYPE_9,
        self::TAG_TYPE_11,
    ];

    /**
     * 商学院培训状态
     */
    const TAG_LIST_SCHOOL = [
        self::TAG_TYPE_12,
        self::TAG_TYPE_13,
    ];
    /**
     * 门店正常解约状态
     */
    const TAG_LIST_CANCEL_STATUS = [
        self::TAG_TYPE_14,
        self::TAG_TYPE_15,
    ];

    /**
     * 工服标签
     */
    const TAG_LIST_WORK_CLOTH = [
        self::TAG_TYPE_19,
        self::TAG_TYPE_20,
    ];

    const TAG_ABNORMAL_1 = 1;
    const TAG_ABNORMAL_2 = 2;
    const TAG_ABNORMAL_3 = 3;
    const TAG_ABNORMAL_4 = 4;
    const TAG_ABNORMAL_5 = 5;
    const TAG_ABNORMAL_6 = 6;
    const TAG_ABNORMAL_7 = 7;
    const TAG_ABNORMAL_8 = 8;


    /**
     * 字段枚举
     *
     * 如果枚举类型变动, 需要通知 @寒烟柔 !!! 不然数据展示会异常
     */
    const TAG_ABNORMAL_MAP_TEXT = [
        self::TAG_ABNORMAL_1 => '商场统一开业',
        self::TAG_ABNORMAL_2 => '加盟商自身原因',
        self::TAG_ABNORMAL_3 => '没有合同',
        self::TAG_ABNORMAL_5 => '商场在建',
        self::TAG_ABNORMAL_6 => '上家未撤场',
        self::TAG_ABNORMAL_7 => '位置风险',
        self::TAG_ABNORMAL_8 => '期铺',
        self::TAG_ABNORMAL_4 => '其它原因',
    ];

    public static function addTags(string $orderNo, int $tagId, string $desc = '', string $remark = '')
    {
        return self::query()->create([
            'orderNo' => $orderNo,
            'tagId'   => $tagId,
            'desc'    => $desc,
            'remark'  => $remark
        ]);
    }

    /**
     * @param $orderNo
     * @param $type
     * @return Builder|Model|object|null
     */
    public static function getInfoByOrderNo($orderNo, $type)
    {
        return self::query()
                   ->when(
                       is_int($type) && $type > 0,
                       function ($query) use ($type) {
                           $query->where("tagId", $type);
                       }
                   )
                   ->when(
                       is_array($type) && count($type),
                       function ($query) use ($type) {
                           $query->whereIn("tagId", $type);
                       }
                   )
                   ->where('orderNo', $orderNo)
                   ->first();
    }

    /**
     * 校验目标 tag 是否存在
     *
     * @param array $needCheck
     * @param array $targetTags
     * @return bool
     */
    public static function checkTags(array $needCheck, array $targetTags)
    {
        foreach ($needCheck as $item) {
            if (in_array($item, $targetTags)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 删除标签
     *
     * @param string $orderNo
     * @param array $tags
     * @return int
     */
    public static function delTags(string $orderNo, array $tags): int
    {
        return self::query()->where('orderNo', $orderNo)
                   ->whereIn('tagId', $tags)
                   ->delete();
    }

    /**
     * @param int $regionSelectionType
     * @param string $region
     * @param string $regionValue
     * @return Builder|mixed
     */
    public static function buildHomeSearchQuery(int $regionSelectionType, string $region, string $regionValue)
    {
        $query = function ($query) {
            $query->where("id", 0);
        };
        switch ($regionSelectionType) {
            case HomeService::REGION_SELECTION_TYPE_1:
                $regionRel = attributeGet(
                    OpenShopOrganizationManageModel::allOrganizationManage()->where("name", $region)->first(),
                    "regionRel",
                    collect([])
                );

                if (!empty($provinceCodes = $regionRel->pluck("provinceCode")->toArray())) {
                    $orderNos = OpenShopAddressModel::query()
                                                    ->whereIn("provinceCode", $provinceCodes)
                                                    ->pluck("orderNo")
                                                    ->toArray();
                    if (!empty($orderNos)) {
                        $query = function ($query) use ($orderNos) {
                            return $query->whereIn("orderNo", $orderNos);
                        };
                    }
                }
                break;
            case HomeService::REGION_SELECTION_TYPE_2:
                $orderNos = OpenShopAddressModel::query()
                                                ->whereIn("provinceCode", $regionValue)
                                                ->pluck("orderNo")
                                                ->toArray();

                if (!empty($orderNos)) {
                    $query = function ($query) use ($orderNos) {
                        return $query->whereIn("orderNo", $orderNos);
                    };
                }
                break;
            case HomeService::REGION_SELECTION_TYPE_3:
                $orderNos = OpenShopAddressModel::query()
                                                ->whereIn("cityCode", $regionValue)
                                                ->pluck("orderNo")
                                                ->toArray();

                if (!empty($orderNos)) {
                    $query = function ($query) use ($orderNos) {
                        return $query->whereIn("orderNo", $orderNos);
                    };
                }
                break;
            case HomeService::REGION_SELECTION_TYPE_0:
                $query = null;
                break;
        }
        return self::query()->when($query, function ($subQuery) use ($query) {
            return $subQuery->where($query);
        });
    }
}
